package service

import (
	"fmt"
	context3 "ksd-social-api/modules/msg/model/context"
	rmodel "ksd-social-api/modules/resume/model"
)

/**
 * 用户购买小册
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
type UserResumeTemplateService struct {
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买专栏信息
 */
func (service *UserResumeTemplateService) SaveUserResumeTemplate(
	OutTradeNo string, TransactionId string, orderJson string, payMethod int, payMethodName string, price string,
	userId uint64, opid uint64, systemId uint64, systemName string) bool {
	// 1： 判断用户是否购买此课程，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyResumeTemplateMapper.CountUserBuyResumeTemplateNo(userId, opid, systemId)
	if isBuyFlag == 0 {
		userBuyResumeTemplate := rmodel.UserBuyResumeTemplate{}
		// 根据课程id查询课程信息
		note, _ := noteMapper.GetById(opid, systemId)
		userVo := userMapper.GetUserByID(userId, systemId)

		userBuyResumeTemplate.SystemId = systemId
		userBuyResumeTemplate.SystemName = systemName
		// 处理返回结果
		userBuyResumeTemplate.Opid = opid
		userBuyResumeTemplate.Title = note.Title
		userBuyResumeTemplate.Cover = note.Img

		userBuyResumeTemplate.UserId = userId
		userBuyResumeTemplate.Username = userVo.Username
		userBuyResumeTemplate.Avatar = userVo.Avatar
		userBuyResumeTemplate.Phone = userVo.Telephone
		userBuyResumeTemplate.Address = userVo.Address
		userBuyResumeTemplate.Nickname = userVo.Nickname
		userBuyResumeTemplate.Code = ""
		userBuyResumeTemplate.PayMethod = payMethod
		userBuyResumeTemplate.PayMethodName = payMethodName
		userBuyResumeTemplate.Tradeno = OutTradeNo
		userBuyResumeTemplate.Orderno = TransactionId
		userBuyResumeTemplate.OrderJson = orderJson
		userBuyResumeTemplate.Price = price
		userBuyResumeTemplateMapper.SaveUserBuyResumeTemplate(userBuyResumeTemplate)

		// 保存消息
		messageMeContext := context3.MessageMeContext{}
		messageMeContext.UserId = userId
		messageMeContext.Title = note.Title
		messageMeContext.Uuid = userVo.Uuid
		messageMeContext.SystemId = systemId
		messageMeContext.SystemName = "系统512"
		messageMeContext.Username = userVo.Nickname
		if note.ChapterFlag == 1 {
			messageMeContext.Url = fmt.Sprintf("https://www.study512.com/note/page/%d", note.Id)
		} else {
			messageMeContext.Url = fmt.Sprintf("https://www.study512.com/note/detail/%d", note.Id)
		}
		messageMeService.SaveMessageMeBuyNote(&messageMeContext)

		return true
	}
	return false
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  判断用户是否购买此小册
 */
func (service *UserResumeTemplateService) CountUserBuyResumeTemplate(userId uint64, noteId uint64, systemId uint64) bool {
	// 1： 判断用户是否购买此小册，如果购买过了就就不要去保存订单
	isBuyFlag := userBuyResumeTemplateMapper.CountUserBuyResumeTemplateNo(userId, noteId, systemId)
	return isBuyFlag == 0
}

/*
*
统计用户已经支付小册的金额
*/
func (service *UserResumeTemplateService) SumUserByResumeTemplate(specailId uint64, userId uint64, systemId uint64) float64 {
	return userBuyResumeTemplateMapper.SumUserByResumeTemplate(specailId, userId, systemId)
}
